<?php
/**
 * 小程序前端接口
 */
namespace app\controllers;

use Yii;
use yii\base\Controller;
use app\lib\Code;
use app\lib\CacheConf;

/**
 */
class ApiBaseController extends Controller
{
    public function beforeAction($action)
    {
        header('content-type:application:json;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:POST');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        
        if (!parent::beforeAction($action)) {
            return false;
        }
        
        //6小时内自动登录
        $cookie = Yii::$app->request->cookies;
        if ($cookie['uid']){ 
            Yii::$app->session['uid'] = $cookie['uid'];
        } else {
            Yii::$app->session['uid'] = '';
        }
//         Yii::$app->session['uid'] = 2;
        
        //判断是否有 session
//         $uid = Yii::$app->session['uid'];
//         if (!$uid) {
//             //判断是否有 cookie
//             $cookie = Yii::$app->request->cookies;
//             if ($cookie['uid']){
//                 Yii::$app->session['uid'] =  $cookie['uid']->value;
//             }
//         }
     
        // other custom code here
         $this->tempSetFormIdList();
        return true; // or false to not run the action
    }
    //默认返回json 数据
    public function afterAction($action, $result)
    {
             $result = parent::afterAction($action, $result);
             formatJson(Code::SUCCESS,'',$result);
    }
    /**
     * 记录用户的交互次数用于发送模板消息
     */
    public function tempSetFormIdList(){
           $get=Yii::$app->request->get();
         if(!empty($get['open_id']) && !empty($get['form_id'])){
             $key=sprintf(CacheConf::DOCTOR_WX_TEMP_FORM_OPENID,$get['open_id']);
            Yii::$app->cache->lpush($key,$get['form_id']);
         }
    }
    
    
    
}